package com.bandi.tharshesnows.persistence;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.bandi.tharshesnows.persistence.Metadata.*;

class Database extends SQLiteOpenHelper {

  private static final String TAG = Database.class.getSimpleName();
  public static final String DB_NAME = "tharshesnows.db";
  private static final int DB_VERSION = 1;

  private SQLiteDatabase db;

  Database(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
  }

  public SQLiteDatabase open() {
    return db = getWritableDatabase();
  }

  public void close() {
    if (db.isOpen()) {
      db.close();
    }
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
    try {
      db.execSQL("CREATE TABLE " + CountyMetadata.TABLE_NAME + " (" + CountyMetadata._ID + " INTEGER PRIMARY KEY, "
          + CountyMetadata.NAME + " TEXT, " + CountyMetadata.STARRED + " INTEGER DEFAULT 0, " + CountyMetadata.LATITUDE
          + " TEXT, " + CountyMetadata.LONGITUDE + " TEXT, " + CountyMetadata.MAP_IMG + " TEXT);");
      db.execSQL("CREATE TABLE " + LocalityMetadata.TABLE_NAME + " (" + LocalityMetadata._ID + " INTEGER PRIMARY KEY, "
          + LocalityMetadata.COUNTY_ID + " INTEGER, " + LocalityMetadata.NAME + " TEXT, " + LocalityMetadata.STARRED
          + " INTEGER DEFAULT 0, " + LocalityMetadata.UPDATED + " INTEGER);");
      db.execSQL("CREATE TABLE " + SkirunMetadata.TABLE_NAME + " (" + SkirunMetadata._ID + " INTEGER PRIMARY KEY, "
          + SkirunMetadata.LOCALITY_ID + " INTEGER, " + SkirunMetadata.NAME + " TEXT, " + SkirunMetadata.CANONS
          + " INTEGER DEFAULT 0, " + SkirunMetadata.DESTINATION_ALT + " REAL, " + SkirunMetadata.DIFFICULTY + " TEXT, "
          + SkirunMetadata.INCLINATION + " REAL, " + SkirunMetadata.LATITUDE + " TEXT, " + SkirunMetadata.LENGTH
          + " REAL, " + SkirunMetadata.LOADED + " INTEGER DEFAULT 0, " + SkirunMetadata.LONGITUDE + " TEXT, "
          + SkirunMetadata.METEO_URL + " TEXT, " + SkirunMetadata.NIGHTMODE + " INTEGER DEFAULT 0, "
          + SkirunMetadata.REMOTE_URL + " TEXT, " + SkirunMetadata.SCHEDULE + " TEXT, " + SkirunMetadata.SKI_TRANSPORTS
          + " TEXT, " + SkirunMetadata.SKIRUNSTATUS + " TEXT, " + SkirunMetadata.SNOW_HEIGHT + " REAL, "
          + SkirunMetadata.STARRED + " INTEGER DEFAULT 0, " + SkirunMetadata.STARTING_ALT + " REAL, "
          + SkirunMetadata.UPDATED + " INTEGER, " + SkirunMetadata.WIDTH + " REAL, " + SkirunMetadata.MAP_IMG
          + " TEXT, " + SkirunMetadata.OBSERVATIONS + ", TEXT);");
      db.execSQL("CREATE TABLE " + PictureMetadata.TABLE_NAME + " (" + PictureMetadata._ID + " INTEGER PRIMARY KEY, "
          + PictureMetadata.IMAGE_PATH + " TEXT, " + PictureMetadata.IMAGE_URL + " TEXT, " + PictureMetadata.SKIRUN_ID
          + " INTEGER, " + PictureMetadata.THUMB_PATH + " TEXT, " + PictureMetadata.THUMB_URL + " TEXT);");
    }
    catch (Throwable t) {
      Log.e(TAG, "Failed to create database.", t);
    }
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

  }

}
